﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class VICC_JJRULES
    {
        public double RGroupValidate(DataRow[] Rules, DataRow dwMJob, DataRow dwJob)
        {
            double rgPoints = 0; 
            double ponts = 0;
  
            foreach (DataRow rule in Rules)
            {
               
                string logic = rule["LOGIC"].ToString();
                string PROPERTY = rule["PROPERTY"].ToString();
                string NVALUE = dwJob[PROPERTY].ToString();
                string CVALUE = dwMJob[PROPERTY].ToString();
                double POINTS = Convert.ToDouble(rule["MINUTES"].ToString());
                double LOWPOINTS = Convert.ToDouble(rule["LOWPOINTS"].ToString());
                double HIGHPOINTS = Convert.ToDouble(rule["HIGHPOINTS"].ToString());
                double RATIO = Convert.ToDouble(rule["RATIOM"].ToString());
                string DIFF = rule["DIFF"].ToString();
                ponts = POINTS * RATIO;
                double Tdiff = 0;
                rule["JJVALUE"] = 0;
                switch (logic)
                {
                    case "=":
                        if (CVALUE.Trim() == NVALUE.Trim())
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        } 
                        break;
                    case ">":
                        if (double.Parse(NVALUE) > double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        } 
                        break;
                    case ">=":
                        if (double.Parse(NVALUE) >= double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        } 
                        break;
                    case "<":
                        if (double.Parse(NVALUE) < double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        } 
                        break;
                    case "T<>": 
                        Tdiff = Math.Abs(double.Parse(NVALUE) - double.Parse(CVALUE));  
                        rule["JJVALUE"] = Tdiff * POINTS ;
                        rgPoints += Tdiff * POINTS ; 
                        break;
                    case "<=":
                        if (double.Parse(NVALUE) <= double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        } 
                        break;
                    case "<>":
                        if (NVALUE != CVALUE)
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        } 
                        break;
                    default:
                        break;
                }
                Tracer.Info("Rules", "dwMJob:" + dwMJob["ABAID"].ToString() + ",dwJob:" + dwJob["ABAID"].ToString() + ",rule:" + rule["RULEID"].ToString()+ ",rgPoints"+ rgPoints);
                
            }

            return rgPoints;
        }
        public void RValidate(DataTable Rules, DataRow dwMachine, DataRow dwMJob, DataRow dwJob)
        {
            if (dwMJob == null)
                return;
            string MACHINEID = dwMachine["MACHINEID"].ToString().Trim();
            dwMachine["JJVALUE"] = 0;
            double rPoint = 0;
            double mPoints = 0;
            bool IsFond = false;
            var qRule = from rule in Rules.AsEnumerable()
                        group rule by new
                        {
                            PT = rule["RGROUP"].ToString(),
                        }
                            into g
                        select new
                        {
                            KEY = g.Key,
                        };
            foreach (var Grule in qRule)
            {
                IsFond = true;
                string Group = Grule.KEY.PT;
                string filter = "RGROUP ='" + Group + "'";
                string sortOrder = "RGROUP ASC,RULEID ASC";
                DataRow[] rows = Rules.Select(filter, sortOrder);
                rPoint = RGroupValidate(rows, dwMJob, dwJob);
                mPoints += rPoint;
                dwMachine["JJVALUE"] = mPoints;
            }
            if (!IsFond)
            {
                dwMachine["JJVALUE"] = 100;
            }
        }

        public double RCOSTGroupValidate(DataRow[] Rules, DataRow dwMJob, DataRow dwJob)
        {
            double rgPoints = 0; 
            double ponts = 0;
            foreach (DataRow rule in Rules)
            {
                string logic = rule["LOGIC"].ToString();
                string PROPERTY = rule["PROPERTY"].ToString();
                string NVALUE = dwJob[PROPERTY].ToString();
                string CVALUE = dwMJob[PROPERTY].ToString();
                double POINTS = Convert.ToDouble(rule["POINTS"].ToString());
                double LOWPOINTS = Convert.ToDouble(rule["LOWPOINTS"].ToString());
                double HIGHPOINTS = Convert.ToDouble(rule["HIGHPOINTS"].ToString());
                double RATIO = Convert.ToDouble(rule["RATIOP"].ToString());
                double MAXLIMIT = Convert.ToDouble(rule["MAXLIMIT"].ToString());
                string DIFF = rule["DIFF"].ToString();
                ponts = POINTS * RATIO;
                double Tdiff = 0;
                switch (logic)
                {
                    case "=":
                        if (CVALUE.Trim() == NVALUE.Trim())
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else
                        {
                            rule["JJVALUE"] = 0;
                        }
                        break;
                    case ">":
                        if (double.Parse(NVALUE) > double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else
                        {
                            rule["JJVALUE"] = 0;
                        }
                        break;
                    case ">=":
                        if (double.Parse(NVALUE) >= double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else
                        {
                            rule["JJVALUE"] = 0;
                        }
                        break;
                    case ">!"://跟系统当前日期比较

                        if (double.Parse(NVALUE) >= (double.Parse(date) + double.Parse(DIFF)))
                        {
                            //在当前日期的两日内交期的，忽略
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else//超过两天，或新任务交期，早于统当前日期
                        {
                            rule["JJVALUE"] = 0;
                            if (double.Parse(NVALUE) >= double.Parse(date))//超过两天
                            {
                                if (double.Parse(NVALUE) > double.Parse(CVALUE))
                                {
                                    rule["JJVALUE"] = ponts;
                                    rgPoints += ponts;
                                }
                            }
                        }
                        break;
                    case "<":
                        if (double.Parse(NVALUE) < double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else
                        {
                            rule["JJVALUE"] = 0;
                        }
                        break;
                    case "T<>": 
                        Tdiff = double.Parse(NVALUE) - double.Parse(CVALUE);
                        if (WD == 1)//上升通道
                        {
                            ponts = Tdiff * POINTS * HIGHPOINTS;
                            if (ponts > MAXLIMIT)
                                ponts = MAXLIMIT;
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                            if (Tdiff > 0)
                            {
                                ponts = Tdiff * POINTS * LOWPOINTS;
                                if (ponts > MAXLIMIT)
                                    ponts = MAXLIMIT;
                                rule["JJVALUE"] = ponts;
                                rgPoints += ponts;
                            }
                        }
                        else//下降过程
                        {
                            ponts = Tdiff * POINTS * LOWPOINTS;
                            if (ponts > MAXLIMIT)
                                ponts = MAXLIMIT;
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                            if (Tdiff > 0)
                            {
                                ponts = Tdiff * POINTS * HIGHPOINTS;
                                if (ponts > MAXLIMIT)
                                    ponts = MAXLIMIT;
                                rule["JJVALUE"] = ponts;
                                rgPoints += ponts;
                            }
                        }
                        break;
                    case "<=":
                        if (double.Parse(NVALUE) <= double.Parse(CVALUE))
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else
                        {
                            rule["JJVALUE"] = 0;
                        }
                        break;
                    case "<>":
                        if (NVALUE != CVALUE)
                        {
                            rule["JJVALUE"] = ponts;
                            rgPoints += ponts;
                        }
                        else
                        {
                            rule["JJVALUE"] = 0;
                        }
                        break;
                    default:
                        break;
                }
            }
            return rgPoints;
        }
        public int WD = 0;
        public void RCOSTValidate(DataTable Rules, DataRow dwMachine, DataRow dwMJob, DataRow dwJob)
        {
           
            if (dwMJob == null)
                return;
            string MACHINEID = dwMachine["MACHINEID"].ToString().Trim();
            dwMachine["JJVALUE"] = 0;
            double rPoint = 0;
            double mPoints = 0;
            bool IsFond = false;
            var qRule = from rule in Rules.AsEnumerable()
                        group rule by new
                        {
                            PT = rule["RGROUP"].ToString(),
                        }
                            into g
                        select new
                        {
                            KEY = g.Key,
                        };
            foreach (var Grule in qRule)
            {
                IsFond = true;
                string Group = Grule.KEY.PT;
                string filter = "RGROUP ='" + Group + "'";
                string sortOrder = "RGROUP ASC,RULEID ASC";
                DataRow[] rows = Rules.Select(filter, sortOrder); 
                rPoint = RCOSTGroupValidate(rows,  dwMJob, dwJob);
                mPoints += rPoint;
                dwMachine["JJVALUE"] = mPoints;
            }
            if (!IsFond)
            {
                dwMachine["JJVALUE"] = 100;
            }
        }
    }
}
